﻿using System;

namespace Less.Core.Model
{
    public class User : IId
    {
        private DateTime _failedPasswordAttemptWindowStart;
        private DateTime _failedPasswordAnswerAttemptWindowStart;
        private string _identifier = Guid.Empty.ToString();
        private string password;
        public int Id { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                if (password.HasValue() || password != value)
                {
                    password = value;
                    LastPasswordChangedDate = DateTime.Now;
                }
            }
        }
        public string PasswordQuestion { get; set; }
        public string PasswordAnswer { get; set; }
        public string Comment { get; set; }
        public bool IsApproved { get; set; }
        public bool IsLockedOut { get; set; }
        public DateTime CreationDate { get; set; }
        public DateTime LastLoginDate { get; set; }
        public DateTime LastActivityDate { get; set; }
        public DateTime LastPasswordChangedDate { get; set; }
        public DateTime LastLockoutDate { get; set; }
        public int FailedPasswordAttemptCount { get; set; }
        public DateTime FailedPasswordAttemptWindowStart
        {
            get
            {
                if (_failedPasswordAttemptWindowStart == DateTime.MinValue)
                    _failedPasswordAttemptWindowStart = DateTime.Now;
                return _failedPasswordAttemptWindowStart;
            }
            set
            {
                _failedPasswordAttemptWindowStart = value;
            }
        }
        public int FailedPasswordAnswerAttemptCount { get; set; }
        public DateTime FailedPasswordAnswerAttemptWindowStart
        {
            get
            {
                if (_failedPasswordAnswerAttemptWindowStart == DateTime.MinValue)
                    _failedPasswordAnswerAttemptWindowStart = DateTime.Now;
                return _failedPasswordAnswerAttemptWindowStart;
            }
            set
            {
                _failedPasswordAnswerAttemptWindowStart = value;
            }
        }
        public string Identifier
        {
            get
            {
                if (_identifier == Guid.Empty.ToString())
                    _identifier = Guid.NewGuid().ToString();
                return _identifier;
            }
            set
            {
                _identifier = value;
            }
        }
        public User()
        {
        }
        public User(int id, string userName, string email, string password, string passwordQuestion, string passwordAnswer, string comment, bool isApproved,
                    bool isLockedOut, DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate, DateTime lastPasswordChangedDate,
                    DateTime lastLockoutDate, int failedPasswordAttemptCount, DateTime failedPasswordAttemptWindowStart, int failedPasswordAnswerAttemptCount,
                    DateTime failedPasswordAnswerAttemptWindowStart)
        {
            Id = id;
            UserName = userName;
            Email = email;
            Password = password;
            PasswordQuestion = passwordQuestion;
            PasswordAnswer = passwordAnswer;
            Comment = comment;
            IsApproved = isApproved;
            IsLockedOut = isLockedOut;
            CreationDate = creationDate;
            LastLoginDate = lastLoginDate;
            LastActivityDate = lastActivityDate;
            LastPasswordChangedDate = lastPasswordChangedDate;
            LastLockoutDate = lastLockoutDate;
            FailedPasswordAttemptCount = failedPasswordAttemptCount;
            FailedPasswordAttemptWindowStart = failedPasswordAttemptWindowStart;
            FailedPasswordAnswerAttemptCount = failedPasswordAnswerAttemptCount;
            FailedPasswordAnswerAttemptWindowStart = failedPasswordAnswerAttemptWindowStart;
        }
    }
}